package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.ResponseResult;
import com.web.entity.Student;
import com.web.service.StudentService;
import com.web.service.impl.StudentServiceImpl;

import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加学生数据接口
 */
@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {

    @Resource
    StudentService studentService ;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得参数
        String name = req.getParameter("name");
        String sage = req.getParameter("age");
        String gender = req.getParameter("gender");
        String cellphone = req.getParameter("cellphone");
        String address = req.getParameter("address");
        int age = 0;
        if(sage != null){
            age = Integer.parseInt(sage);
        }
        //添加学生
        studentService.addStudent(new Student(0,name,age,gender,address,cellphone));
        //返回信息给前端
        resp.getWriter().write(JSON.toJSONString(new ResponseResult(200,"OK",null)));
    }
}
